package mnc.game.tifo;

import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.CookieSyncManager;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ImageView;

public class WebViewActivity extends Activity {

	private Intent intent;
	private Context context;
	private WebView webview;
	private ImageView imageview, title_im;

	private String url;
	private String kind;
	
	
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);   
	    intent = getIntent();
	    url      =  intent.getStringExtra("URL");
	    kind     =  intent.getStringExtra("KIND");
	    

	    setContentView(R.layout.webview);
	    
	    context = WebViewActivity.this;
	    
	    webview = (WebView) findViewById(R.id.tl_webview);
	    
	    webview.setWebViewClient(new WebViewClient()); 
//	    webview.getSettings().setPluginsEnabled(true);
	    webview.getSettings().setJavaScriptEnabled(true);
	    webview.getSettings().setSupportZoom(true);
	    webview.getSettings().setBuiltInZoomControls(true);
	    webview.getSettings().setBlockNetworkImage(false);
	    webview.getSettings().setLoadsImagesAutomatically(true);
	    webview.getSettings().setUseWideViewPort(true);
	    webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
//       	webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
	    webview.getSettings().setUseWideViewPort(true);  
	    webview.getSettings().setLoadWithOverviewMode(true);
	    
	    webview.loadUrl(url);
	    webview.setWebViewClient(new MyWebViewClient());
	    webview.setWebChromeClient(new MyWebChromeClient());  
	}
	@Override
	public void onPause(){
    	super.onPause();
    	CookieSyncManager.getInstance().stopSync();
    }
	
    /// WebChromeClient
    private class MyWebChromeClient extends WebChromeClient {
     	  @Override
    	  public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
    		 final JsResult finalJsResult = result;             
    		 new AlertDialog.Builder(view.getContext())
    		     .setMessage(message)
    		     .setPositiveButton(android.R.string.ok, 
    		    		 new AlertDialog.OnClickListener() {                 
        			 @Override                 
        			 public void onClick(DialogInterface dialog, int which) {                     
        				 finalJsResult.confirm();                 
        			 }            
    	     })
    	     .setCancelable(false)
    	     .create()
    	     .show();     
    		 
     
             return true; 
    	  }
    	  
    	  @Override 
    	  public void onProgressChanged(WebView view, int progress) {
    		 WebViewActivity.this.setProgress(progress * 100);
    	  }
    	  
    	  @Override     
    	  public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {         
   		      final JsResult res = result;      
              new AlertDialog.Builder(view.getContext())
              .setTitle("?�림")
              .setMessage(message)
              .setPositiveButton("OK", 
                      new DialogInterface.OnClickListener() {
                          @Override
						public void onClick(
                                  DialogInterface dialog,
                                  int which) {
                              res.confirm();
                          }})
              .setNegativeButton("NO", 
                      new DialogInterface.OnClickListener() {
                          @Override
						public void onClick(
                                  DialogInterface dialog,
                                  int which) {
                              res.cancel();
                          }})
              .setOnCancelListener(
                      new DialogInterface.OnCancelListener() {
                          @Override
						public void onCancel(
                                  DialogInterface dialog) {
                              res.cancel();
                          }
                      })
              .show();
    		  return true;     
    	  } 
    	    
        @Override
        public boolean onCreateWindow(WebView view, boolean dialog,
                   boolean userGesture, Message resultMsg) {

        	   
    		WebView childView = new WebView(WebViewActivity.this);         
       		final WebSettings settings = childView.getSettings();         
          		settings.setJavaScriptEnabled(true); 
                settings.setJavaScriptCanOpenWindowsAutomatically(true);
                settings.setSupportMultipleWindows(true);
                settings.setSupportZoom(true);
                settings.setBuiltInZoomControls(true);
          		childView.setWebChromeClient(this);    
          		WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
          		transport.setWebView(childView);         
          		resultMsg.sendToTarget(); 
                     
                return true; 
           }
    }	
	
	
	// WebViewClient 
    private class MyWebViewClient extends WebViewClient {
     @Override
     public boolean shouldOverrideUrlLoading(WebView view, String url) {
     	 Log.i("url", url);

//         if(url.startsWith( "/amigos/web/image/list.aspx")) {
//             String param[] = url.split("&");
//             String workNo[] = param[1].split("=");
//
//             intent.putExtra("TRLSC-WORK-NO", workNo[1]);
//             startActivity(intent) ;
//         	 return true ;   
//  			
//         } else if(url.startsWith("file://sdcard/AA_MA_AD/files/form_save_offline.aspx")) {
//
//
//        }
       	 
         view.loadUrl(url);
         
         return true;

     }

 	    
     @Override
     public void onLoadResource (WebView view, String url)  
     {                     
       super.onLoadResource(view,url); 
     }     
     

     @Override
     public void onFormResubmission(WebView view, Message dontResend, Message resend) {
         resend.sendToTarget();
     }
     
     @Override
		public void onPageStarted(WebView view, String url, Bitmap favicon)
		{
//     	if(!url.endsWith("version.html")) {
//     		loadDialog();
//     	}
                                                                    
     	super.onPageStarted(view, url, favicon);   

		}  
     
     @Override
		public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)
		{
			handler.proceed();
		}
 }
	 @Override 
	 public boolean onKeyDown(int keyCode, KeyEvent event) { 
	     if((keyCode == KeyEvent.KEYCODE_BACK)) {
    	     WebViewActivity.this.setResult(Activity.RESULT_OK, intent);
	         //return true; 
	     } else if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 
	     	 webview.goBack(); 
	         return true; 
	     }
	     return super.onKeyDown(keyCode, event); 
	 }
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
    
}
